package com.fyoung.coordinatorlayoutdemo;

import android.graphics.Color;
import android.os.Bundle;
import android.support.design.widget.AppBarLayout;
import android.support.design.widget.CollapsingToolbarLayout;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.support.v7.widget.helper.ItemTouchHelper;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.WindowManager;
import android.widget.LinearLayout;

import com.fyoung.coordinatorlayoutdemo.listener.ItemDragHelperCallback;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity  {

    private static final String TAG = "MainActivity";
    private AppBarLayout appBarLayout;
    private RecyclerView mRecyclerView;
    Toolbar toolbar;

    CollapsingToolbarLayout collapsingToolbar;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //沉浸式状态栏
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
        toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        
        initView();

        //给页面设置工具栏
        if (collapsingToolbar != null) {
            //设置隐藏图片时候ToolBar的颜色
            collapsingToolbar.setContentScrimColor(Color.parseColor("#11B7F3"));
            //设置工具栏标题
            collapsingToolbar.setTitle("从入门到放弃");
        }

        List<ItemData> list = initData();

        mRecyclerView.setHasFixedSize(true);
        LinearLayoutManager manager = new LinearLayoutManager(this, LinearLayout.VERTICAL, false);
        mRecyclerView.setLayoutManager(manager);
        //mRecyclerView.setAdapter(new LifeAdapter(list));
        //长按拖拽打开
        ItemDragHelperCallback callback = new ItemDragHelperCallback() {
            @Override
            public boolean isLongPressDragEnabled() {
                return true;
            }
        };
        ItemTouchHelper helper = new ItemTouchHelper(callback);
        helper.attachToRecyclerView(mRecyclerView);
        //设置adapter
        LifeAdapter adapter = new LifeAdapter(list);
        mRecyclerView.setAdapter(adapter);
        //设置RecyclerView的每一项的点击事件
        adapter.setRecyclerViewOnItemClickListener(new LifeAdapter.RecyclerViewOnItemClickListener() {
            @Override
            public void onItemClickListener(View view, int position) {
                Snackbar.make(view, "点击了：" + position, Snackbar.LENGTH_SHORT).show();
            }
        });
        //设置RecyclerView的每一项的长按事件
        adapter.setOnItemLongClickListener(new LifeAdapter.RecyclerViewOnItemLongClickListener() {
            @Override
            public boolean onItemLongClickListener(View view, int position) {
                Snackbar.make(view, "长按了：" + position, Snackbar.LENGTH_SHORT).show();
                return true;
            }
        });
    }

    /**
     * 初始化View
     */
    private void initView() {
        collapsingToolbar = (CollapsingToolbarLayout) findViewById(R.id.collapsing_toolbar);
        mRecyclerView = (RecyclerView) findViewById(R.id.recycler_content);
        appBarLayout = (AppBarLayout) findViewById(R.id.abl_bar);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.main,menu);
        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()){
            case R.id.home:
                break;
            case R.id.about:
                new AlertDialog.Builder(this).setMessage("作者:阿钟\ncsdn地址:http://blog.csdn.net/a_zhon/").show();
                break;
        }
        return super.onOptionsItemSelected(item);
    }

    private List<ItemData> initData() {
        List<ItemData> list = new ArrayList<>();
        for (int i = 1; i <= 50; i++) {
            ItemData cellData = new ItemData("程序员 +" + i, "不想当项目经理的程序员不是个好程序员" + i);
            list.add(cellData);
        }
        return list;

    }


}
